﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.UI.Game.InGame.Gameplay;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Epitech.Krisk.Xna.UI.Game.InGame
{
    public enum PanelState
    {
        None,
        Ok,
        Cancel
    }

    public class LeftPanel : AGameObject
    {
        #region Fields

        protected Texture2D _panel;
        protected SpriteFont _font;
        protected Vector2 _posNomPays;
        protected Vector2 _posRenfort;
        protected float _textscale = 0.75f;

        protected Rectangle _rec;

        private Texture2D _soldier;
        private Rectangle _soldierrec;

        private Texture2D _tank;
        private Rectangle _tankrec;

        private Texture2D _warplane;
        private Rectangle _warplanerec;

        private bool _anim = true;

        public Army Renfort { get; set; }

        protected const double TimeAnim = 0.25;

        private TimeSpan _animateStart = TimeSpan.Zero;

        protected readonly Territory _territory;
        protected readonly Player _player;

        private Dictionary<String, Rectangle> _buttons;

        protected Army Temoin;

        #endregion

        public LeftPanel(Territory territory, Player player)
        {
            _territory = territory;
            _player = player;
            End = false;
            ToDelete = false;
            Renfort = new Army();
            State = PanelState.None;
        }

        public bool End { get; set; }

        public override void LoadContent()
        {
            _panel = Content.Load<Texture2D>("Game/BarGame");
            _warplane = Content.Load<Texture2D>("Game/Avion_Sprite");
            _tank = Content.Load<Texture2D>("Game/Char_Sprite");
            _soldier = Content.Load<Texture2D>("Game/Soldat_Sprite");
            _font = Content.Load<SpriteFont>("Font/defaultfont");

            _rec = new Rectangle(-307, 135, 307, 480);


            while (_font.MeasureString(_territory.Name).X * _textscale > _rec.Width - 25)
                _textscale -= 0.05f;
            
            _soldierrec = new Rectangle(_rec.X, _rec.Y + 15 + 150, 17, 45);
            _soldierrec.X += 60 - _soldierrec.Width/2;
            _tankrec = new Rectangle(_rec.X, _rec.Y + 15 + 150, 72, 45);
            _tankrec.X += 60 - _tankrec.Width / 2;
            _tankrec.Y += _soldierrec.Height + 20;
            _warplanerec = new Rectangle(_rec.X, _rec.Y + 15 + 150, 63, 45);
            _warplanerec.X += 60 - _warplanerec.Width / 2;
            _warplanerec.Y += _tankrec.Height + 20 + _soldierrec.Height + 20;
            _posNomPays = new Vector2(_rec.X + (_rec.Width - 7) / 2 - _font.MeasureString(_territory.Name).X * _textscale / 2, _rec.Y + 15 + 10);
            _posRenfort = new Vector2(_rec.X + 10, _rec.Y + 100);

            _buttons = new Dictionary<string, Rectangle>
                           {
                               {
                                   "[ OK ]",
                                   new Rectangle(_rec.X + _rec.Width/2 - (int) (_font.MeasureString("[ OK ]").X * 0.5f/2),
                                                 _rec.Y + 15 + 360, (int) (_font.MeasureString("[ OK ]").X * 0.5f),
                                                 (int) (_font.MeasureString("[ OK ]").Y * 0.5f))
                                   },
                               {
                                   "[ ANNULER ]",
                                   new Rectangle(_rec.X + _rec.Width/2 - (int) (_font.MeasureString("[ ANNULER ]").X * 0.5f/2),
                                   _rec.Y + 15 + 360 + (int) (_font.MeasureString("[ ANNULER ]").Y * 0.5f),
                                   (int) (_font.MeasureString("[ ANNULER ]").X * 0.5f),
                                   (int) (_font.MeasureString("[ ANNULER ]").Y * 0.5f))
                                   }
                           };
        }

        public override void UnloadContent()
        {
        }

        public override void HandleInput(InputState input)
        {
            PlayerIndex tmp;

            if (input.IsNewMouseButtonDown(MouseButton.Left)
                || input.IsNewButtonPress(Buttons.A, null, out tmp))
            {
                if (_buttons["[ OK ]"].Contains((int)input.MousePosition().X, (int)input.MousePosition().Y))
                    State = PanelState.Ok;
                else if (_buttons["[ ANNULER ]"].Contains((int)input.MousePosition().X, (int)input.MousePosition().Y))
                    State = PanelState.Cancel;
            }
            if (input.IsNewButtonPress(Buttons.B, null, out tmp) 
                || input.IsNewKeyPress(Keys.Enter, null, out tmp))
            {
                State = PanelState.Ok;
            }

            // Ajout d'unité
           
            if (Temoin - Renfort > 0)
                if (input.IsNewKeyPress(Keys.P, null, out tmp)
                    || input.IsNewButtonPress(Buttons.RightShoulder, null, out tmp))
                    Renfort++;
            if (Renfort > 0)
                if (input.IsNewKeyPress(Keys.M, null, out tmp)
                    || input.IsNewButtonPress(Buttons.LeftShoulder, null, out tmp))
                    Renfort--;
        }

        public PanelState State { get; set; }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if (_anim && !End)
            {
                if (_animateStart == TimeSpan.Zero)
                    _animateStart = gameTime.TotalGameTime;
                var depl = (int)((double)_rec.Width / TimeAnim * gameTime.ElapsedGameTime.TotalSeconds);
                _rec.X += depl;
                if (_rec.X > 0)
                    _rec.X = 0;
                else
                {
                    _soldierrec.X += depl;
                    _tankrec.X += depl;
                    _warplanerec.X += depl;
                    _posNomPays.X += depl;
                    _posRenfort.X += depl;
                    var tmp = new Rectangle(_buttons["[ OK ]"].X + depl, _buttons["[ OK ]"].Y, _buttons["[ OK ]"].Width, _buttons["[ OK ]"].Height);
                    _buttons["[ OK ]"] = tmp;
                    tmp = new Rectangle(_buttons["[ ANNULER ]"].X + depl, _buttons["[ ANNULER ]"].Y, _buttons["[ ANNULER ]"].Width, _buttons["[ ANNULER ]"].Height);
                    _buttons["[ ANNULER ]"] = tmp;
                }

                

                if (gameTime.TotalGameTime - _animateStart > TimeSpan.FromSeconds(TimeAnim))
                {
                    _anim = false;
                    _animateStart = TimeSpan.Zero;
                }
            }
            if (End)
            {
                if (_animateStart == TimeSpan.Zero)
                    _animateStart = gameTime.TotalGameTime;
                var depl = (int)((double)_rec.Width / TimeAnim * gameTime.ElapsedGameTime.TotalSeconds);
                _rec.X -= depl;
                if (_rec.X > 0)
                    _rec.X = 0;
                _soldierrec.X -= depl;
                _tankrec.X -= depl;
                _warplanerec.X -= depl;
                _posNomPays.X -= depl;
                _posRenfort.X -= depl;
                var tmp = new Rectangle(_buttons["[ OK ]"].X - depl, _buttons["[ OK ]"].Y, _buttons["[ OK ]"].Width, _buttons["[ OK ]"].Height);
                _buttons["[ OK ]"] = tmp;
                tmp = new Rectangle(_buttons["[ ANNULER ]"].X - depl, _buttons["[ ANNULER ]"].Y, _buttons["[ ANNULER ]"].Width, _buttons["[ ANNULER ]"].Height);
                _buttons["[ ANNULER ]"] = tmp;
                if (gameTime.TotalGameTime - _animateStart > TimeSpan.FromSeconds(TimeAnim))
                    ToDelete = true;
            }
        }

        public bool ToDelete { get; set; }

        public override void Draw(GameTime gameTime)
        {
            Rectangle area = ViewPort.Bounds;

            SpriteBatch.Begin();
            SpriteBatch.Draw(_panel, _rec, Color.White);
            SpriteBatch.DrawString(_font, _territory.Name, _posNomPays, Color.Black, 0f, Vector2.Zero, _textscale, SpriteEffects.None, 0f);
            //SpriteBatch.DrawString(_font, String.Format("Renfort         {0}", _player.ReinforcementArmy.TotalSoldier() - Renfort.TotalSoldier()), _posRenfort, Color.Black, 0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0f);
            SpriteBatch.Draw(_soldier, _soldierrec, Color.White);
            SpriteBatch.Draw(_tank, _tankrec, Color.White);
            SpriteBatch.Draw(_warplane, _warplanerec, Color.White);
            Vector2 rpos = new Vector2(_tankrec.X + 140, _soldierrec.Y);
            SpriteBatch.DrawString(_font, Renfort.Soldier.ToString(), rpos, Color.Black);
            rpos = new Vector2(_tankrec.X + 140, _tankrec.Y);
            SpriteBatch.DrawString(_font, Renfort.Tank.ToString(), rpos, Color.Black);
            rpos = new Vector2(_tankrec.X + 140, _warplanerec.Y);
            SpriteBatch.DrawString(_font, Renfort.WarPlane.ToString(), rpos, Color.Black);

            foreach (KeyValuePair<string, Rectangle> button in _buttons)
            {
                Vector2 pos = new Vector2(button.Value.X, button.Value.Y);
                SpriteBatch.DrawString(_font, button.Key, pos, new Color(208, 44, 6), 0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0f);
            }


            SpriteBatch.End();
        }
    }
}
